package io;

//import cn.hutool.core.io.FileUtil;

import java.io.*;
import java.nio.channels.FileChannel;

public class CopyFile {

    public static void main(String[] args) {
        File fileCopyFileStream = new File("E:\\document\\io\\深入Nginx100讲脑图_copyFileStream.jpg");
        File fileCopyBufferedFileStream = new File("E:\\document\\io\\深入Nginx100讲脑图_copyBufferedFileStream.jpg");
        File fileCopyHutool = new File("E:\\document\\io\\深入Nginx100讲脑图_hutool.jpg");
        File fileCopyNio = new File("E:\\document\\io\\深入Nginx100讲脑图_nio.jpg");

        if (fileCopyFileStream.exists()) {
            try {
                fileCopyFileStream.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        File file = new File("E:\\document\\io\\深入Nginx100讲脑图.jpg");

        long start1 = System.currentTimeMillis();
        fileCopyFileStream(file, fileCopyFileStream);
        long hTime = System.currentTimeMillis() - start1;

        System.out.println("不使用Buffered所花时间：" + hTime);
        long start3 = System.currentTimeMillis();
        fileCopyBufferedFileStream(file, fileCopyBufferedFileStream);
        long hbTime = System.currentTimeMillis() - start3;
        System.out.println("使用Buffered所花时间：" + hbTime);

//        long start5 = System.currentTimeMillis();
//        FileUtil.copyFilesFromDir(file, fileCopyHutool, true);
//        long huTime = System.currentTimeMillis() - start5;
//        System.out.println("使用hutool工具所花时间："+huTime);// 底层使用java.nio.file.Files.copy()方法

        long start7 = System.currentTimeMillis();
        copyByNio(file, fileCopyNio);
        long nioTime = System.currentTimeMillis() - start7;
        System.out.println("使用NIO所花时间：" + nioTime);
    }

    public static void fileCopyFileStream(File file, File fileCopyFileStream) {

        try (
                FileInputStream inputStream = new FileInputStream(file);
                FileOutputStream outputStream = new FileOutputStream(fileCopyFileStream)
        ) {
            byte[] arr = new byte[1024];
            int len;
            while ((len = inputStream.read(arr)) != -1) {
                outputStream.write(arr, 0, len);
            }
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void fileCopyBufferedFileStream(File file, File fileCopyFileStream) {

        try (
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
                FileOutputStream fos = new FileOutputStream(fileCopyFileStream);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos)
        ) {
            byte[] arr = new byte[1024];
            int len;
            while ((len = bufferedInputStream.read(arr)) != -1) {
                bufferedOutputStream.write(arr, 0, len);
            }
            bufferedOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyByNio(File file, File fileCopyNio) {

        try (
                FileInputStream fileInputStream = new FileInputStream(file);
                FileOutputStream fileOutputStream = new FileOutputStream(fileCopyNio);
        ) {
            FileChannel input = fileInputStream.getChannel();
            FileChannel output = fileOutputStream.getChannel();
            output.transferFrom(input, 0, input.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
